<?php

namespace App\HttpController\XAppSystem\AppAdmin\Admin;


use App\Common\Log;
use App\Common\OA\OA;
use App\HttpController\ControllerAdmin;
use App\Model\t_s_app;
use App\Model\t_wx_reply;
use XWX\Common\H;
use XWX\Common\Validate\Validate;

class AppAdmin extends ControllerAdmin
{
    public function index()
    {
        $this->funcSetTitle('应用管理');


        $compKw = $this->reqParam('compKw');


        $app_type = ['common'];
        $app_type[] = $this->funcGetX__config()->aconfig_type;


        $query = $this->db()
            ->whereIn('app_type', $app_type)
            ->where('app_enable', 1)
            ->where('app_do_reply', 1)
            ->orderBy('app_order');

        if (H::funcStrHasAnyText($compKw))
        {
            $query->where('app_name', "%{$compKw}%", 'LIKE');
        }

        $query_count = clone $query;

        $list = $query->get(t_s_app::table(), [$this->funcGetPageIndexXPage(), $this->funcGetPageSize()]);
        $count = $query_count->count(t_s_app::table());


        //二次加工
        $list_ = [];
        foreach ($list as $v)
        {
            $data = $v;

            $data['id'] = $v['app_id'];

            $list_[] = $data;
        }


        $this->commonR()->setData("list", $list_);
        $this->commonR()->setData("count", $count);


        $this->funcWriteCommonR();
    }


    public function edit()
    {
        $entity_app = t_s_app::funcLoad($this->db(), $this->getID());

        $this->funcSetTitle("{$entity_app->app_name}-配置");

        $key = $this->reqParam('key');
        if (H::funcStrIsNullOrEmpty($key))
        {
            $key = '0';
        }


        $arr_reply = $this->db()
            ->where('reply_x_id', $this->funcGetX())
            ->where('reply_app_id', $this->getID())
            ->where('reply_key', $key)
            ->getOne(t_wx_reply::table());

        $data = [
            'reply_pic_imgs' => [],
            'reply_share_pic_imgs' => [],
        ];
        if ($arr_reply != null)
        {
            $entity_reply = new t_wx_reply($arr_reply);

            $data_append = [
                'id' => $entity_reply->funcGetID(),
                'reply_pic_imgs' => OA::funcGetUploadFileList($entity_reply->reply_pic),
                'reply_share_pic_imgs' => OA::funcGetUploadFileList($entity_reply->reply_share_pic),
                '__word' => $entity_app->app_id,
                '__oauth_base' => $this->funcGetAppWx()->funcGetOAuthUrl('__word__', 0),
                '__oauth_url' => $this->funcGetAppWx()->funcGetOAuthUrl($this->getID(), 0),
                '__oauth_url_x' => $this->funcGetAppWx()->funcGetOAuthUrl($this->getID(), 1),
            ];
            $data = array_merge($entity_reply->toArray(), $data_append);
        }
        $data['app_idx'] = $entity_app->app_id;


        $this->commonR()->setData('data', $data);
        $this->funcWriteCommonR();
    }

    public function save()
    {
        if (H::funcStrIsNullOrEmpty($this->reqParam('app_idx')))
        {
            return $this->funcWriteErr(-100, '缺少参数');
        }


        $v = new Validate();
        $v->addColumn('reply_title')
            ->required()
            ->lenMax(50);
        $v->addColumn('reply_desc')
            ->required()
            ->lenMax(500);
//        $v->addColumn('reply_app_file')
//            ->required();

        if ($this->reqParam('reply_do_share') == 1)
        {
            $v->addColumn('reply_share_title')
                ->required()
                ->lenMax(50);
            $v->addColumn('reply_share_desc')
                ->required()
                ->lenMax(500);
        }


        if (!$v->validate($this->reqParam(), true))
        {
            return $this->funcWriteValidate($v->getErrors());
        }


        $ignore_field = [];
        if ($this->funcIsAdminEdit())
        {
            $ignore_field = ['reply_app_id'];
        }


        $this->funcSave(t_wx_reply::class, $ignore_field, false);


        $this->funcWriteCommonR();
    }


    public function funcSaveBefore($entity)
    {
        if ($this->funcIsAdminAdd())
        {
            $entity->reply_app_id = $this->reqParam('app_idx');
            $entity->reply_key = '0';
            $entity->reply_type = 'msg';
        }

        Log::funcLog(__METHOD__, $entity->toArray());
    }
}